import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './assets/style.css'

import dataView from '@jiaminghi/data-view'
import MessageBox from './components/MessageBox.vue'

Vue.use(dataView)

// 注册全局MessageBox组件
Vue.component('MessageBox', MessageBox)

Vue.config.productionTip = false

const app = new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

// 创建全局MessageBox实例
const MessageBoxConstructor = Vue.extend(MessageBox)
let messageBoxInstance = null

// 全局$messageBox API
Vue.prototype.$messageBox = {
  show(options = {}) {
    if (!messageBoxInstance) {
      messageBoxInstance = new MessageBoxConstructor()
      messageBoxInstance.$mount()
      document.body.appendChild(messageBoxInstance.$el)
    }
    return messageBoxInstance.show(options)
  },

  info(message, title = '提示信息') {
    return this.show({
      title,
      message,
      type: 'info'
    })
  },

  transfer(data, title = '罪犯投送信息') {
    return this.show({
      title,
      type: 'transfer',
      data: {
        source: data.source || '未知来源',
        target: data.target || '未知目标',
        count: data.count || '0名'
      }
    })
  },

  toast(options = {}) {
    const toastOptions = {
      type: 'toast',
      toastType: options.toastType || 'info',
      position: options.position || 'top-right',
      autoHide: options.autoHide !== false,
      hideDelay: options.hideDelay || 3000,
      ...options
    }
    return this.show(toastOptions)
  },

  showGenderToast(data = {}, position = 'top-right') {
    return this.toast({
      title: '罪犯性别年龄分布',
      toastType: 'gender',
      position,
      data: {
        male: data.male || '49663',
        female: data.female || '5461',
        minorMale: data.minorMale || '414',
        minorFemale: data.minorFemale || '27'
      }
    })
  }
}

// 全局$message API (模拟Element UI)
Vue.prototype.$message = {
  success(message) {
    return Vue.prototype.$messageBox.show({
      title: '成功',
      message,
      type: 'success'
    })
  },

  error(message) {
    return Vue.prototype.$messageBox.show({
      title: '错误',
      message,
      type: 'error'
    })
  },

  warning(message) {
    return Vue.prototype.$messageBox.show({
      title: '警告',
      message,
      type: 'warning'
    })
  },

  info(message) {
    return Vue.prototype.$messageBox.show({
      title: '提示',
      message,
      type: 'info'
    })
  }
}

// 应用启动后检查用户认证状态
app.$store.dispatch('user/checkAuth')
